const express = require('express');
const userList = require('./data/index.json')
const app = express();
app.use(express.json());
app.use(function(req,res,next){
    if(req.headers.host ==="shenxiaoxu.com:8080"){
        //判断是否为自身的服务发起的 配置中changeOrigin:true
        next();
    }else{
        console.log(req.headers.host)
        res.json({
            ok:-1,
            msg:"网络连接错误"
        })
    }
});

app.post("/login",(req,res)=>{
    const {username,password} = req.body;
    const userInfo = userList.find(v=>v.username === username && v.password===password);
    if(userInfo){
        res.json({
            ok:1,
            userInfo,
            msg:"登陆成功"
        })
    }else{
        res.json({
            ok:-1,
            msg:"账号或密码错误"
        })
    }
})
app.listen(8080,()=>{
    console.log("success");
})